
/* Opens and parses a .egg model file, displaying it
 * using OpenGL
 *
 * Written by: Peter Ingulli
 */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

#include "rigidbody.h"

GLuint texture[1];
int animationAngle = 0;
RigidBody cube;

void doubleArrayAllocation(float*& ar, int arNum);
void init(int argc, char *argv[]);
 bool NeHeLoadBitmap(LPTSTR szFileName, GLuint &texid);
 void display();
 void idle();
void cubeSetup();


int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		std::printf("Usage: filename\n");
		return -1;
	}

	cubeSetup();

	init( argc, argv );

	return 0;
}

void init(int argc, char *argv[])
{
	glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowSize( 600, 600 );
    glutInitWindowPosition( 100, 100 );
    glutCreateWindow( argv[0] );
    glClearColor ( 0, 0, 0, 0 );
    glEnable( GL_DEPTH_TEST );
    glShadeModel( GL_SMOOTH );
    glEnable( GL_TEXTURE_2D );
    
    glutDisplayFunc( display );
	glutIdleFunc( idle );
	
	/*if(NeHeLoadBitmap("panda-model.bmp", texture[0])==FALSE){
		std::printf("Could not load texture.\n");
		return;
	}*/
    glutMainLoop();
}

void cubeSetup()
{
	std::string obj_file = "cube.obj";
	cube.load(obj_file);
	cube.setPos(0.f, 5.f, 0.f);
	
	float **colors = new float*[6];
	colors[0] = new float[3];
	colors[0][0]=1.f; colors[0][1]=0.f; colors[0][2]=0.f;
	colors[1] = new float[3];
	colors[1][0]=0.f; colors[1][1]=1.f; colors[1][2]=0.f;
	colors[2] = new float[3];
	colors[2][0]=0.f; colors[2][1]=0.f; colors[2][2]=1.f;
	colors[3] = new float[3];
	colors[3][0]=1.f; colors[3][1]=1.f; colors[3][2]=0.f;
	colors[4] = new float[3];
	colors[4][0]=0.f; colors[4][1]=1.f; colors[4][2]=1.f;
	colors[5] = new float[3];
	colors[5][0]=1.f; colors[5][1]=0.f; colors[5][2]=1.f;

	cube.setColors(6, colors);
}

void display()
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	//glOrtho( -700, 700, -700, 700, -700, 700 ) ;
	glOrtho( -7, 7, -7, 7, -7, 7 ) ;
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	glRotatef(animationAngle, 0.f, 1.f, 0.f);
	//egg.draw();
	//obj.draw();
	//obj.printData();
	cube.draw();

	glutSwapBuffers();
}

void idle()
{
	animationAngle += 1.0;
	glutPostRedisplay();
}

bool NeHeLoadBitmap(LPTSTR szFileName, GLuint &texid)					// Creates Texture From A Bitmap File
{
	HBITMAP hBMP;														// Handle Of The Bitmap
	BITMAP	BMP;														// Bitmap Structure

	glGenTextures(1, &texid);											// Create The Texture
	hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );

	if (!hBMP)															// Does The Bitmap Exist?
		return FALSE;													// If Not Return False

	GetObject(hBMP, sizeof(BMP), &BMP);									// Get The Object
																		// hBMP:        Handle To Graphics Object
																		// sizeof(BMP): Size Of Buffer For Object Information
																		// &BMP:        Buffer For Object Information

	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);								// Pixel Storage Mode (Word Alignment / 4 Bytes)

	// Typical Texture Generation Using Data From The Bitmap
	glBindTexture(GL_TEXTURE_2D, texid);								// Bind To The Texture ID
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Min Filter
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Mag Filter
	glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);

	DeleteObject(hBMP);													// Delete The Object

	return TRUE;														// Loading Was Successful
}

